# stoker_mysql:  MySQL/ROCKS interface for stoker
#
# Author:   Hanjun Xian (hxian@clemson.edu)
#           Linton Abraham (labraha@clemson.edu)
#           Heather Harton (hkeown@clemson.edu)
#           Hardik Barot (hbarot@clemson.edu)
#           Mike Murphy (mamurph@cs.clemson.edu)
#
# Revision: 13 November 2008
#
#   Copyright 2007-2008 Clemson University
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
# This material is based upon work supported under a National Science
# Foundation Graduate Research Fellowship. Any opinions, findings,
# conclusions or recommendations expressed in this publication are those of
# the author(s) and do not necessarily reflect the views of the National
# Science Foundation.

# TODO: probably still need better error handling

import sys, stoker_base, MySQLdb
from stoker_defines import EXIT_ERROR_MYSQL, HOSTNAME_KEY, IPV4_KEY, MAC_KEY
#
from stoker_base import pr_warning, pr_error
#
from stoker_messages import MSG_MYSQL_ERROR
#


class MySQLMachines(stoker_base.MachineSourceBase):
   def __init__(self, stoker_config):
      stoker_base.MachineSourceBase.__init__(self, stoker_config)
      #
      self.groups = {}
      self.CLI_NAME = 'mysql'
      #
      host = self.config.getParam(CFG_MYSQL_HOST, DFLT_MYSQL_HOST)
      db = self.config.getParam(CFG_MYSQL_DB, DFLT_MYSQL_DB)
      user = self.config.getParam(CFG_MYSQL_USER, DFLT_MYSQL_USER)
      passwd = self.config.getParam(CFG_MYSQL_CRED, DFLT_MYSQL_CRED)
      self.db = MySQLdb.connect(host=host, db=db, user=user, passwd=passwd)
      self.cursor = self.db.cursor()
      #
      self.am_query = self.config.getParam(CFG_MYSQL_QUERY_MACHINES, \
         DFLT_MYSQL_QUERY_MACHINES)
      self.ag_query = self.config.getParam(CFG_MYSQL_QUERY_GROUPS, \
         DFLT_MYSQL_QUERY_GROUPS)
      self.hr_query = self.config.getParam(CFG_MYSQL_QUERY_RESOLVE_HOSTS, \ 
         DFLT_MYSQL_QUERY_RESOLVE_HOSTS)
      self.gr_query = self.config.getParam(CFG_MYSQL_QUERY_RESOLVE_GROUPS, \ 
         DFLT_MYSQL_QUERY_RESOLVE_GROUPS)
   def resolveTargets(self, targets):
      return resolve_targets(targets, self.cursor, self.hr_query, self.gr_query)
   def getAllMachines(self):
      return list_all_machines(self.cursor, self.am_query)
   def getAllGroups(self):
      return list_all_groups(self.cursor, self.ag_query)
#

# List machines by name, DO NOT RESOLVE to IP
def list_all_machines(cursor, query):
   machines = []

   cursor.execute(query)
   result=cursor.fetchall()

   for record in result:
      item = record[0]
      machines.append(item)
   return machines
#

def list_all_groups(cursor, query):
   cursor.execute(query)
   result = cursor.fetchall()
   return result
#

# Actual target resolution
def resolve_targets(target_list, cursor, host_query, group_query):
   resolved = []
   unresolved = []
   item={}
   hostnamelist=[]
   grouplist=[]

   for target in target_list:
      if(target.isdigit()):
         grouplist.append(target)
      else:
         hostnamelist.append(target)
   #

   if(grouplist!=[]):
      groupsql = group_query.replace('%targets', ','.join(grouplist))
      cursor.execute(groupsql)
      result = cursor.fetchall()
      for record in result:
         item[MAC_KEY] = record[0]
         item[IPV4_KEY] = record[1]
         item[HOSTNAME_KEY] = record[2]
         resolved.append(item)
   else:
      sql = host_query.replace('%targets', ','.join(hostnamelist))
      cursor.execute(sql)
      result = cursor.fetchall()
      for record in result:
         item[MAC_KEY] = record[0]
         item[IPV4_KEY] = record[1]
         item[HOSTNAME_KEY] = record[2]
         resolved.append(item)
   #
   
   return (resolved, unresolved)
#
